package com.engr.lyx.blog.biz.repository.article;

import com.engr.lyx.blog.biz.repository.BaseDao;
import com.engr.lyx.blog.client.entity.ArticleVisitLog;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
 * @program: blog
 * @desc:
 * @author: lyx
 * @date: 2019-05-09 20:18
 **/
public interface ArticleVisitLogRepository extends BaseDao<ArticleVisitLog> {

    /**
     * @desc 查询用户点击量最高的文章
     * @author lyx
     * @create 2019-05-18
     **/
    @Query(value = "SELECT a.id,a.user_id userId,a.article_id articleId,a.article_title title,a.brief brief," +
            "COUNT(article_id) views FROM article_visit_log a GROUP BY a.user_id,a.article_id " +
            "HAVING user_id = ?1 LIMIT 3", nativeQuery = true)
    List<Map<String, Object>> findTopArticle(Long userId);

    /**
     * 根据文章id查询访问量
     */
    @Query(value = "SELECT COUNT(*) as views FROM `article_visit_log` GROUP BY article_id HAVING article_id = ?1", nativeQuery = true)
    Integer findViewDetailByArticleIds(Long articleId);

    /**
     * @desc 根据文章id查找详情
     * @param articleId
     * @author lyx
     * @return
     */
    List<ArticleVisitLog> findByArticleId(Long articleId);


}
